﻿using AutoMapper;
using MediatR;
using SheepStorage.API.Application.Command.ProviderManageCommand;
using SheepStorage.API.Baseclass;
using SheepStorage.Domain.Entity;
using SheepStorage.Service.Interface;

namespace SheepStorage.API.Application.CommandHandlers.ProviderManageHandler
{
    public class ProviderManageCreateHandler:IRequestHandler<ProviderManageCreateCommand,Results<int>>
    {
        private readonly IProviderManageService _providerService;
        private readonly IMapper _mapper;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="providerService"></param>
        /// <param name="mapper"></param>
        public ProviderManageCreateHandler(IProviderManageService providerService,IMapper mapper)
        {
            _providerService = providerService;
            _mapper = mapper;
        }

        /// <summary>
        /// 供应商添加
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Results<int>> Handle(ProviderManageCreateCommand request, CancellationToken cancellationToken)
        {
            var result = _mapper.Map<ProviderManage>(request);

            return Results<int>.DataResult(await _providerService.AddProviderManage(result));
        }
    }
}
